<?php

class MRNotifications {

	public static $EVENT_NEW_REGISTER = "_new";
	public static $EVENT_UPDATED_REGISTER = "_update";
	public static $EVENT_DELETED_REGISTER = "_delete";
	public static $EVENT_NEW_NOTE = "_note_new";
	public static $EVENT_UPDATED_NOTE = "_note_update";
	public static $EVENT_DELETED_NOTE = "_note_delete";
	
	private $typesOfNotifiedUsers = array("owner", "handler", "monitor", "notes", "viewer", "reporter", "updater", "developer", "manager", "admin");
	
	public function getUsersNotifiedByEvent($event, $bug_data) {
		$usersToBeNotified = "";
		$allUsersByAccessLevel = $this->getAllUsersDividedByAccessLevel();
		
		
		foreach($this->typesOfNotifiedUsers as $typeUser) {
			if (plugin_config_get($typeUser . $event) == ON) {


				switch($typeUser) {
					case $this->typesOfNotifiedUsers[0]: {
						$usersToBeNotified .= user_get_field($bug_data->reporter_id, 'email') . ",";
						error_log("Relator: " . $usersToBeNotified . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
						break;
					}
					case $this->typesOfNotifiedUsers[1]: {
						$usersToBeNotified .= ($bug_data->handler_id != 0 ? user_get_field($bug_data->handler_id, 'email')  . "," : "");
						error_log("Tratador: " . $usersToBeNotified . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
						break;
					}
					case $this->typesOfNotifiedUsers[2]: {
						$usersToBeNotified .= $this->getUsersMonitoringBug($bug_data->id);
						error_log("Monitor: " . $usersToBeNotified . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
						break;
					}
					case $this->typesOfNotifiedUsers[3]: {
						$usersToBeNotified .= $this->getUsersAddNoteToBug($bug_data->id);
						error_log("Anotador: " . $usersToBeNotified . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
						break;
					}
					case $this->typesOfNotifiedUsers[4]: {
						$usersToBeNotified .= $allUsersByAccessLevel[$this->typesOfNotifiedUsers[4]];
						error_log("View: " . $usersToBeNotified . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
						break;
					}
					case $this->typesOfNotifiedUsers[5]: {
						$usersToBeNotified .= $allUsersByAccessLevel[$this->typesOfNotifiedUsers[5]];
						error_log("Reporter: " . $usersToBeNotified . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
						break;
					}
					case $this->typesOfNotifiedUsers[6]: {
						$usersToBeNotified .= $allUsersByAccessLevel[$this->typesOfNotifiedUsers[6]];
						error_log("Updater: " . $usersToBeNotified . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
						break;
					}
					case $this->typesOfNotifiedUsers[7]: {
						$usersToBeNotified .= $allUsersByAccessLevel[$this->typesOfNotifiedUsers[7]];
						error_log("Developer: " . $usersToBeNotified . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
						break;
					}
					case $this->typesOfNotifiedUsers[8]: {
						$usersToBeNotified .= $allUsersByAccessLevel[$this->typesOfNotifiedUsers[8]];
						error_log("Manager: " . $usersToBeNotified . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
						break;
					}
					case $this->typesOfNotifiedUsers[9]: {
						$usersToBeNotified .= $allUsersByAccessLevel[$this->typesOfNotifiedUsers[9]];
						error_log("Admin: " . $usersToBeNotified . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
						break;
					}
				}
				
			}
		}

		$usersToBeNotified = ($usersToBeNotified{strlen($usersToBeNotified) - 1} == ','
				? substr($usersToBeNotified, 0, strlen($usersToBeNotified) - 1)
				: $usersToBeNotified);
		return $usersToBeNotified;
	}
	
	private function getAllUsersDividedByAccessLevel() {
		
		$allUsersByAccessLevel = array("viewer" => "", "reporter" => "", "updater" => "", "developer" => "", "manager" => "", "admin" => "");
		
		
		$query = "SELECT email, access_level AS level 
				FROM mantis_user_table
				WHERE enabled = 1
				AND email != ''
				AND access_level IN (" . VIEWER . ", " . REPORTER . ", " . UPDATER . ", " . DEVELOPER . ", " . MANAGER . ", " . ADMINISTRATOR . ")";
		
		$result = db_query($query);
		
		while($row = db_fetch_array($result)) {
			switch($row['level']) {
				case VIEWER: {
					$allUsersByAccessLevel['viewer'] .= $row['email'] . ",";
					break;
				}
				case REPORTER: {
					$allUsersByAccessLevel['reporter'] .= $row['email'] . ",";
					break;
				}
				case UPDATER: {
					$allUsersByAccessLevel['updater'] .= $row['email'] . ",";
					break;
				}
				case DEVELOPER: {
					$allUsersByAccessLevel['developer'] .= $row['email'] . ",";
					break;
				}
				case MANAGER: {
					$allUsersByAccessLevel['manager'] .= $row['email'] . ",";
					break;
				}
				case ADMINISTRATOR: {
					$allUsersByAccessLevel['admin'] .= $row['email'] . ",";
					break;
				}
			}
		}

		return $allUsersByAccessLevel;
		
	}
	
	private function getUsersMonitoringBug($bug_id) {
		$users_monitoring = bug_get_monitors($bug_id);

		if (!empty($users_monitoring)) {

			$users_monitoring_email = "";
			foreach ($users_monitoring as $monitor) {
				$users_monitoring_email .= (user_get_field($monitor, 'email') != "" ? user_get_field($monitor, 'email') . ",": "");
			}

			return $users_monitoring_email;
			
		} else {
			return "";
		}
	}
	
	private function getUsersAddNoteToBug($bug_id) {
		$users_addnote = bugnote_get_all_bugnotes($bug_id);
		
		if (!empty($users_addnote)) {

			$users_addnote_email = "";
			foreach ($users_addnote as $user) {
				$users_addnote_email .= (user_get_field($user->reporter_id, 'email') != "" ? user_get_field($user->reporter_id, 'email') . "," : "");
			}

			return $users_addnote_email;
		} else {
			return "";
		}
	}
	
}